package cn.itcast.tanhua.controller;


import cn.itcast.mongo.UserFocus;
import cn.itcast.tanhua.server.SmallVideosService;
import cn.itcast.vo.PageResult;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    /**
     * 发布视频
     *  接口路径：POST
     *  请求参数：
     *      videoThumbnail：封面图
     *      videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }


    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 视频点赞
     * @param Id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") ObjectId Id) {
        Integer likeCount = videosService.likeComment(new ObjectId(String.valueOf(Id)));
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 视频取消点赞
     * @param Id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String Id) {
        Integer likeCount = videosService.likedelete(Id);
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 视频评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity Comments(@PathVariable("id") ObjectId vid, @RequestBody Map map) {

        String comment = (String)map.get("comment");
        videosService.commentsTest(vid,comment);
        return ResponseEntity.ok(null);
    }
    /**
     * 评论列表
     */
    @GetMapping("{id}/comments")
    public ResponseEntity Comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   @PathVariable("id") ObjectId id){
        PageResult pr = videosService.Comments(id,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") ObjectId id){
       videosService.userFocus(id);
        return ResponseEntity.ok(null);
    }


}